<template>
  <div style="margin: 10px">
    <div>
      <Button style="margin-top: 10px" long @click="submit">保存数据</Button>
      <Button style="margin-top: 10px" long @click="add_count_down"
        >添加倒数日</Button
      >
      <Input
        style="margin-top: 10px"
        v-model="count_down_data.text"
        placeholder="天地转，光阴迫。一万年太久，只争朝夕。"
      />
      <List border size="large" style="margin-top: 10px">
        <ListItem v-for="(item, i) in count_down_data.count_down_list" :key="i">
          <Input v-model="item.title" placeholder="输入事件名称" style="" />
          <DatePicker
            format="yyyy-MM-dd HH:mm"
            show-week-numbers
            :model-value="item.time"
            type="datetime"
            placeholder="选择日期"
            style=""
            @on-change="get_time($event, item)"
          />
          <a style="margin: 0 5px" @click="del_count_down(item)"> 删除</a>
        </ListItem>
      </List>
    </div>
  </div>
</template>

<script>
import putBuffer from '@/api/API_Uploading_Directory_Tree'
import { PATH_count_down } from '@/mylibs/define'
export default {
  data() {
    return {
      count_down_data: {
        text: '多少事，从来急；天地转，光阴迫。一万年太久，只争朝夕。',
        count_down_list: [
          {
            title: '',
            time: '',
            time_ms: 0,
            year: 0,
            month: 0,
            day: 0,
            h: 0,
            m: 0
          }
        ]
      }
    }
  },

  methods: {
    get_time(e, item) {
      if (e.length == 0) return
      item.time = e
      let year = Number(e.split('-')[0])
      let month = Number(e.split('-')[1]) - 1
      let day = Number(e.split('-')[2].split(' ')[0])
      let h = Number(e.split('-')[2].split(' ')[1].split(':')[0])
      let m = Number(e.split('-')[2].split(' ')[1].split(':')[1])

      // console.log(year, month, day, h, m)
      item.time_ms = new Date(year, month, day, h, m, 0).getTime()
    },
    async submit() {
      let { ok } = await putBuffer(
        'put',
        PATH_count_down,
        this.count_down_data,
        false
      )

      if (ok) {
        this.$Notice.success({ desc: '保存成功' })
      } else {
        this.$Notice.error({ desc: '保存失败:' + err })
      }
    },

    async get_count_down() {
      let { ok, err, res } = await putBuffer('get', PATH_count_down, false)
      if (ok) {
        if (res) {
          this.count_down_data = res
        }
      } else {
        this.$Notice.error({ desc: err })
      }
    },

    // 添加
    add_count_down() {
      this.count_down_data.count_down_list.unshift({
        title: '',
        time: '',
        time_ms: 0
      })
    },
    // 删除
    del_count_down(item) {
      this.count_down_data.count_down_list.splice(
        this.count_down_data.count_down_list.indexOf(item),
        1
      )
    }
  },
  activated() {
    this.get_count_down()
  }
}
</script>

<style></style>
